package com.lstimeline.utility.DataManager;

import com.lstimeline.utility.AeSimpleSHA1;

import java.sql.*;
/**
 * Handles database interaction for the register servlet
 * @author Harry
 */
public class RegisterDataManager extends AbstractDataManager {
	/**
	 * Register a user
	 * The inputs should have gone through validation.
	 * Username and Email should have been confirmed to be unique.
	 * The username should have been converted to uppercase.
	 * usergroup:
	 *	-1: unverified
	 *   0: verified
	 *   5: admin
	 * @return
	 * 		whether the registration is successful
	 */
	public static boolean registerUser(String username, String password, String email) {
		boolean isSuccessful = false;
		Connection conn = getConnection();
		if(conn != null) {
			PreparedStatement ps = null;
			try {
				String queryString = "INSERT INTO user (regtime, usergroup, username, password, email) VALUES (NOW(), -1, ?, ?, ?)";
				
				String uUsername = username.toUpperCase();
				//digest the password
				String ePassword = AeSimpleSHA1.SHA1(password);
				String lEmail = email.toLowerCase();
				
				ps = conn.prepareStatement(queryString);
				ps.setString(1, uUsername);
				ps.setString(2, ePassword);
				ps.setString(3, lEmail);
				
				int rowCount = ps.executeUpdate();

				isSuccessful = rowCount == 1;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					ps.close();
					closeConnection(conn);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return isSuccessful;
	}
	
	/**
	 * Determines if an username is available
	 */
	public static boolean isUsernameAvailable(String username) {
		boolean isAvailable = false;
		Connection conn = getConnection();
		if(conn != null) {
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				String queryString = "SELECT COUNT(username) FROM user WHERE username=?";
				
				String uUsername = username.toUpperCase();
				
				ps = conn.prepareStatement(queryString);
				ps.setString(1, uUsername);
				
				rs = ps.executeQuery();
				
				int usernameNum = 0;
				while(rs.next()) {
					usernameNum = rs.getInt(1);
				}
				
				isAvailable = usernameNum == 0;
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				try {
					rs.close();
					ps.close();
					closeConnection(conn);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} 
		}
		return isAvailable;
	}
	
	/**
	 * Determines if an email address is unused.
	 */
	public static boolean isEmailUnused(String email) {
		boolean isUnused = false;
		Connection conn = getConnection();
		if(conn != null) {
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				String queryString = "SELECT COUNT(email) FROM user WHERE email=?";
				
				String lEmail = email.toLowerCase();
				
				ps = conn.prepareStatement(queryString);
				ps.setString(1, lEmail);
				
				rs = ps.executeQuery();
				
				int emailNum = 0;
				while(rs.next()) {
					emailNum = rs.getInt(1);
				}
				
				isUnused = emailNum == 0;
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				try {
					rs.close();
					ps.close();
					closeConnection(conn);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} 
		}
		return isUnused;
	}
}
